import React from "react";
import { Form, Input, Button, Toast } from "antd-mobile";
import axios from "axios";
import { useNavigate } from "react-router-dom";
function Index() {
  let nav = useNavigate();
  const onFinish = async (values) => {
    console.log(values);
    let resg = await axios.post("/api/login", values);
    console.log(values);
    const {
      code,
      data:{accessToken,userInfo},
      message,
    } = resg.data;
    if (code === 10000) {
      Toast.show({
        icon: "success",
        content: message,
      });
      localStorage.setItem("token", accessToken);
      localStorage.setItem('userInfo',JSON.stringify(userInfo))
      nav('/home')
    } else {
      Toast.show({
        icon: "fail",
        content: message,
      });
    }
  };
  return (
    <div>
      <Form
        layout="horizontal"
        onFinish={onFinish}
        footer={
          <Button block type="submit" color="primary" size="large">
            提交
          </Button>
        }
      >
        <Form.Header>2212A购票登录</Form.Header>
        <Form.Item
          name="username"
          label="用户名"
          rules={[{ required: true, message: "用户名不能为空" }]}
        >
          <Input onChange={console.log} placeholder="请输入用户名" />
        </Form.Item>
        <Form.Item
          name="password"
          label="密码"
          rules={[{ required: true, message: "密码不能为空" }]}
        >
          <Input onChange={console.log} placeholder="请输入密码" />
        </Form.Item>
      </Form>
    </div>
  );
}

export default Index;
